]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/Tests/BATS Scripts/bats_test_state_dump.sh
mDNSResponder-1096.0.2.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / Tests / BATS Scripts / bats_test_state_dump.sh
1 #!/bin/sh
2 #
3 # bats_test_state_dump.sh
4 # mDNSResponder Tests
5 #
6 # Copyright (c) 2019 Apple Inc. All rights reserved.
7
8
9 # trigger state dump
10 function triger_state_dump {
11 local command_line="$1"
12 output="$($command_line)"
13 if [[ $? -ne 0 ]]; then
14 printf "dns-sd -O exit with non-zero return value, the returned error message is:\n"
15 echo $output
16 return 1
17 fi
18 return 0
19 }
20
21 # trigger state dump and check if the file is created successfully
22 function triger_state_dump_and_check_file {
23 # $1 is the command line used to trigger state dump
24 triger_state_dump "$1"
25 if [[ $? -ne 0 ]]; then
26 return 1
27 fi
28 # the returned result is like the following:
29 # State Dump Is Saved to: /private/var/log/mDNSResponder/mDNSResponder_state_dump_2019-03-01_17-07-10-260-08-00.txt
30 # Time Used: 33 ms
31 # splited the result with '\n'
32 IFS=$'\n' read -r -d '' -a line_being_read <<< "$output"
33 # ${line_being_read[0]} contains "State Dump Is Saved to: /private/var/log/mDNSResponder/mDNSResponder_state_dump_2019-03-01_17-07-10-260-08-00.txt"
34 IFS=":" read -r -d '' -a line_being_splitted <<< "${line_being_read[0]}"
35 # get the path "/private/var/log/mDNSResponder/mDNSResponder_state_dump_2019-03-01_17-07-10-260-08-00.txt"
36 file_name=$(echo "${line_being_splitted[1]}" | xargs)
37 # check if the file exists in the disk
38 if [ ! -f $file_name ]; then
39 printf "State dump is not created under %s\n" $file_name
40 return 1
41 fi
42 return 0
43 }
44
45 # verify that the state dump contains the full content
46 function verify_state_dump_content {
47 # the passed parameter is the file content to be checked
48 local file="$1"
49 # the first line of file should start with "---- BEGIN STATE LOG ----"
50 local file_start_string="---- BEGIN STATE LOG ----"
51 if [[ ! "$file" == "$file_start_string"* ]]; then
52 printf "State dump file does not start with %s\n" "$file_start_string"
53 return 1
54 fi
55
56 # the last line of file should start with "---- END STATE LOG ----"
57 local last_line=$(echo "$file" | tail -n1)
58 local file_end_string="---- END STATE LOG ----"
59 if [[ ! "$last_line" == "$file_end_string"* ]]; then
60 printf "State dump file does not end with %s\n" "$file_end_string"
61 return 1
62 fi
63
64 return 0
65 }
66
67 function test_check_dump_directory {
68 local dump_path="/private/var/log/mDNSResponder"
69 if [ ! -d "$dump_path" ]; then
70 printf "Directory \"%s\" does not exist\n" $dump_path
71 return 1
72 fi
73 local permission=$(stat -f "%OLp" $dump_path)
74 if [ ! $permission == "755" ]; then
75 printf "Directory \"%s\" has incorrect permission. expected=755; actual=%s\n" $dump_path $permission
76 return 1
77 fi
78 local owner=$(ls -ld $dump_path | awk '{print $3}')
79 if [ ! $owner == "_mdnsresponder" ]; then
80 printf "Directory \"%s\" has incorrect owner. expected=_mdnsresponder; actual=%s\n" $dump_path "$owner"
81 return 1
82 fi
83 return 0
84 }
85
86 function test_output_to_plain_txt_file {
87 triger_state_dump_and_check_file "dns-sd -O"
88 if [[ $? -ne 0 ]]; then
89 return 1
90 fi
91 # get the file content
92 local file_content=$(cat $file_name)
93 verify_state_dump_content "$file_content" && rm "$file_name"
94 return $?
95 }
96
97 # tests if "dns-sd -O -compress" works as expected
98 function test_output_to_archive {
99 triger_state_dump_and_check_file "dns-sd -O -compress"
100 if [[ $? -ne 0 ]]; then
101 return 1
102 fi
103 # get the uncompressed file name
104 local file_name_uncompressed=$(tar -tf "$file_name")
105 # unzip the file
106 tar -xf "$file_name" --directory /tmp/
107 # get the file content
108 local file_content=$(cat "/tmp/$file_name_uncompressed")
109 verify_state_dump_content "$file_content" && rm "$file_name" && rm "/tmp/$file_name_uncompressed"
110 return $?
111 }
112
113 # tests if "dns-sd -O -stdout" works as expected
114 function test_output_to_stdout {
115 triger_state_dump "dns-sd -O -stdout"
116 if [[ $? -ne 0 ]]; then
117 return 1
118 fi
119 # delete the last line of output, which is " Time Used: <time> ms"
120 output=$(echo "$output" | sed '$d')
121 verify_state_dump_content "$output"
122 return $?
123 }
124
125 # tests whether the state dump will create at most MAX_NUM_DUMP_FILES, to avoid wasting too much space.
126 function test_dump_limit {
127 # calls "dns-sd -O -compress" for 10 times
128 local counter=1
129 while [ $counter -le 10 ]
130 do
131 triger_state_dump_and_check_file "dns-sd -O -compress"
132 if [[ $? -ne 0 ]];then
133 return 1;
134 fi
135 ((counter++))
136 done
137
138 # $file_name is already initialized when we call "dns-sd -O -compress" above
139 local directory=$(dirname "$file_name")
140 # get the number of files under $directory
141 local file_count=$(ls -Uba1 "$directory" | grep ^mDNSResponder_state_dump_ | wc -l | xargs)
142 # clean up the directory
143 rm -rf ${directory}/*
144 # the number of files should be MAX_NUM_DUMP_FILES, which is defined as 5 in mDNSResponder
145 if [[ $file_count -eq 5 ]]; then
146 return 0
147 else
148 return 1
149 fi
150 }
151
152 ret=0
153 # Functions are put inside an array, use ($test) to evaluate it
154 declare -a tests=("test_check_dump_directory"
155 "test_output_to_plain_txt_file"
156 "test_output_to_archive"
157 "test_output_to_stdout"
158 "test_dump_limit")
159 echo ""
160 echo "----State Dump Test Start, $(date)----"
161 for test in "${tests[@]}"; do
162 echo "running $test:"
163 ($test)
164 if [[ $? -eq 0 ]]; then
165 echo "passed"$'\n' # use $'\n' to print one more newline character
166 else
167 ret=1
168 echo "failed"$'\n'
169 fi
170 done
171 echo "----State Dump Test End, $(date)----"
172 exit $ret